var baseUrl = 'https://a.zhsoch.com/api'

function fetchApi(baseUrl, url, params, method) {
  let token = wx.getStorageSync('token');
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${baseUrl}/${url}`,
      data: params,
      method: method,
      header: {
        'content-type': 'application/json',
        "token": token,
      },
      success: resolve,
      fail: reject
    })
  })
}

function fetchUploadApi(baseUrl, url, params, method) {
  let token = wx.getStorageSync('token');

  return new Promise((resolve, reject) => {
    wx.uploadFile({
      url: `${baseUrl}/${url}`, // 仅为示例，非真实的接口地址
      header: {
        'Content-Type': 'multipart/form-data',
        "token": token,
      },
      formData: {
        method: method //请求方式
      },
      filePath: params,
      name: 'file',
      success: resolve,
      fail: reject
    })
  })
}

module.exports = {
  // 登录
  getLogin: function (params) {
    return fetchApi(baseUrl, 'my/miniapplogin', params, 'POST')
      .then(res => res.data)
  },
  //小程序用户授权后把头像和昵称信息储存到数据库
  miniGetuserinfo: function (params) {
    return fetchApi(baseUrl, 'my/miniGetuserinfo', params, 'POST')
      .then(res => res.data)
  },
  //获取汽车品牌
  getBrand: function (params) {
    return fetchApi(baseUrl, 'index/get_brand', params, 'POST')
      .then(res => res.data)
  },
  //获取品牌id下汽车品牌
  getBrandCate: function (params) {
    return fetchApi(baseUrl, 'index/get_brand_cate', params, 'POST')
      .then(res => res.data)
  },
  //获取汽车品牌外观
  get_outsize: function (params) {
    return fetchApi(baseUrl, 'index/get_outsize', params, 'POST')
      .then(res => res.data)
  },
  //获取汽车品牌内饰
  get_insize: function (params) {
    return fetchApi(baseUrl, 'index/get_insize', params, 'POST')
      .then(res => res.data)
  },
  //添加新车发布列表
  add_new_car_publish: function (params) {
    return fetchApi(baseUrl, 'index/add_new_car_publish', params, 'POST')
      .then(res => res.data)
  },
  //添加新车发布列表
  add_new_car_publish_offer: function (params) {
    return fetchApi(baseUrl, 'index/add_new_car_publish_offer', params, 'POST')
      .then(res => res.data)
  },
  //获取求购列表
  getBuyingMessageList(params) {
    return fetchApi(baseUrl, 'Contribution/index', params, 'POST')
      .then(res => res.data)
  },
  //获取我的求购列表
  getMyBuyingMessageList(params) {
    return fetchApi(baseUrl, 'my/my_manuscript', params, 'POST')
      .then(res => res.data)
  },
  //留言
  comment(params) {
    return fetchApi(baseUrl, 'Contribution/comment', params, 'POST')
      .then(res => res.data)
  },
  //留言详情
  commentDetail(params) {
    return fetchApi(baseUrl, '/contribution/detail', params, 'POST')
      .then(res => res.data)
  },
  //发布、上架车源
  postCar: function (params) {
    return fetchApi(baseUrl, 'index/post_car', params, 'POST')
      .then(res => res.data)
  },
  //求购
  buyingMessage(params) {
    return fetchApi(baseUrl, 'Contribution/post', params, 'POST')
      .then(res => res.data)
  },

  //用户信息
  userInfo: function (params) {
    return fetchApi(baseUrl, 'my/userInfo', params, 'POST')
      .then(res => res.data)
  },
  //文件上传
  upLoad: function (params) {
    return fetchUploadApi(baseUrl, 'common/upload', params, 'POST')
      .then(res => res.data)
  },
  //修改用户信息  
  editUserInfo: function (params) {
    return fetchApi(baseUrl, 'my/edit', params, 'POST')
      .then(res => res.data)
  },
  //获取我的邀请列表
  myInvite: function (params) {
    return fetchApi(baseUrl, 'my/my_invite', params, 'POST')
      .then(res => res.data)
  },
  //获取我的车源
  myCarSource: function (params) {
    return fetchApi(baseUrl, 'my/my_car', params, 'POST')
      .then(res => res.data)
  },
  //生成订单
  createOrder: function (params) {
    return fetchApi(baseUrl, 'order/create_order', params, 'POST')
      .then(res => res.data)
  },
  //订单调起支付
  orderPay: function (params) {
    return fetchApi(baseUrl, 'order/order_pay', params, 'POST')
      .then(res => res.data)
  },
  //订单调起支付
  orderPayPublishCaTop: function (params) {
    return fetchApi(baseUrl, 'order/order_pay_publish_car_top', params, 'POST')
      .then(res => res.data)
  },
  //会员支付列表
  orderList:function(params){
    return fetchApi(baseUrl, 'index/order_list', params, 'POST')
    .then(res => res.data)
  },
  //小程序支付回调
  callBackForWxApp:function(params){
    return fetchApi(baseUrl, 'index/callback_for_wxapp', params, 'POST')
    .then(res => res.data)
  },
  //商品列表
  getGoodsList: function (params) {
    return fetchApi(baseUrl, 'index/search', params, 'POST')
      .then(res => res.data)
  },
  //新车列表
  newCarSearch: function (params) {
    return fetchApi(baseUrl, 'index/new_car_search', params, 'POST')
      .then(res => res.data)
  },
  //获取商品详情页面
  getGoodsDetail: function (params) {
    return fetchApi(baseUrl, 'index/goods_detail', params, 'POST')
      .then(res => res.data)
  },
  //获取报价详情页面
  getBaojiaDetail: function (params) {
    return fetchApi(baseUrl, 'index/baojia_detail', params, 'POST')
      .then(res => res.data)
  },
  //获取商品详情页面
  getNewCarDetail: function (params) {
    return fetchApi(baseUrl, 'index/new_car_publish_detail', params, 'POST')
      .then(res => res.data)
  },
  //搜索历史
  getHistoryName: function (params) {
    return fetchApi(baseUrl, 'index/history_name', params, 'POST')
      .then(res => res.data)
  },
  //清空搜索历史
  clearSearchHistory: function (params) {
    return fetchApi(baseUrl, 'index/history_delete', params, 'POST')
      .then(res => res.data)
  },
  //获取我的浏览记录
  getMyHistory: function (params) {
    return fetchApi(baseUrl, 'my/my_history', params, 'POST')
      .then(res => res.data)
  },
  //获取我的车源分享二维码
  getShareDetail: function (params) {
    return fetchApi(baseUrl, 'index/share_detail', params, 'POST')
      .then(res => res.data)
  },
  userShare: function (params) {
    return fetchApi(baseUrl, 'index/share_huiyuan', params, 'POST')
      .then(res => res.data)
  },
  sourceDel: function (params) {
    return fetchApi(baseUrl, 'index/goods_delete', params, 'POST')
      .then(res => res.data)
  },
  requestDel: function (params) {
    return fetchApi(baseUrl, 'Contribution/delete_qiugou', params, 'POST')
      .then(res => res.data)
  },
  postError: function (params) {
    return fetchApi(baseUrl, 'my/add_error_correction', params, 'POST')
      .then(res => res.data)
  },
  myHelpSell: function (params) {
    return fetchApi(baseUrl, 'my/my_help_sell', params, 'GET')
      .then(res => res.data)
  },
  add_help_sell: function (params) {
    return fetchApi(baseUrl, 'my/add_help_sell', params, 'POST')
      .then(res => res.data)
  },

}